package com.sample.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sample.action.form.UserForm;
import com.sample.javabean.Area;
import com.sample.javabean.Baby;
import com.sample.javabean.Menu;
import com.sample.javabean.Role;
import com.sample.javabean.User;
import com.sample.util.Pager;

/**
 * 用户管理模块
 * 
 * @author 演示
 * 
 */
public class UserAction extends BaseAction {

	private final static Log log = LogFactory.getLog(UserAction.class);
	private UserForm userForm = new UserForm();
	private String userid;
	private String yzcode;
	private Pager<User> pageList;
	private List<Area> areaList;// 定义为成员属性，可以被jvm回收
	private List<Role> roleList;
	private List<Baby> babyList;
	private List<Menu> menuList;
	private String keywords;
	
	// 使用new()操作符实例化领域模型
	private User user = new User();

	/**
	 * 添加用户
	 * 
	 * @return
	 * @throws Exception
	 */
	public String add()throws Exception{
		try{
			log.info("add User:"+userForm.getFullname());
			if(userForm!=null){
				user.setUserid(userForm.getUserid());
				user.setFullname(userForm.getFullname());
				user.setPwd(userForm.getPwd());
				user.setCityno(userForm.getCityno());
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String time = format.format(new Date());
				user.setType(userForm.getType());
				user.setUpdatetime(time);
				user.addUser();	
			}
			return SUCCESS;
		}catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		
	}

	/**
	 * 用户登录
	 * 
	 * @return
	 * @throws Exception
	 */
	public String login() throws Exception {
		try {
			log.info("用户登录:" + yzcode);
			// 获取验证码
			String key_yzm = (String) getSession().getAttribute(
					com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
			boolean flag = false;
			if (userForm != null) {
				user.setUserid(userForm.getUserid());
				user.setPwd(userForm.getPwd());
				flag = user.check();
			}
			if(flag){
				userid = userForm.getUserid();
				menuList = user.findMenuList(userForm.getUserid());
			}
			String targetUrl = flag ? "login" : "error";
			return targetUrl;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("用户名或者密码有误:" + e.getMessage());
		}

	}

	/**
	 * 用户退出
	 * 
	 * @return
	 * @throws Exception
	 */
	public String logOut() throws Exception {
		try {
			log.info("退出系统:");
			// 会话强制失效
			getSession().invalidate();
			return "logout";
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("用户名或者密码有误:" + e.getMessage());
		}

	}

	/**
	 * 显示用户记录列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String listUser() throws Exception {
		try {

			pageList = user.list(page, size, userForm,keywords);
			return "listUser";
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}

	}

	public String inputUser() throws Exception {
		try {
			Area area = new Area();
			// 加载网点信息
			areaList = area.findAll();
			
			Role role = new Role();
			//加载权限信息
			roleList = role.findAll();

			return "inputUser";
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}

	}

	public String editUser() throws Exception {
		try {
			user.setUserid(userid);
			user.load();
			Role role = new Role();
			//加载权限信息
			roleList = role.findAll();
			return "editUser";
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String edit() throws Exception {
		try {
			if (userForm != null) {
				user.setUserid(userForm.getUserid());
				user.setFullname(userForm.getFullname());
				user.setPwd(userForm.getPwd());
				user.setCityno(userForm.getCityno());
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String time = format.format(new Date());
				user.setType(userForm.getType());
				user.setUpdatetime(time);

				user.editUser();
			}
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String del() throws Exception {
		try {
			if (userForm != null) {
				user.setUserid(userid);
				user.delUser();
			}
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public UserForm getUserForm() {
		return userForm;
	}

	public void setUserForm(UserForm userForm) {
		this.userForm = userForm;
	}

	public String getYzcode() {
		return yzcode;
	}

	public void setYzcode(String yzcode) {
		this.yzcode = yzcode;
	}

	public Pager<User> getPageList() {
		return pageList;
	}

	public void setPageList(Pager<User> pageList) {
		this.pageList = pageList;
	}

	public User getUser() {
		return user;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}
	public List<Area> getAreaList() {
		return areaList;
	}

	public void setAreaList(List<Area> areaList) {
		this.areaList = areaList;
	}

	public List<Menu> getMenuList() {
		return menuList;
	}

	public void setMenuList(List<Menu> menuList) {
		this.menuList = menuList;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<Baby> getBabyList() {
		return babyList;
	}

	public void setBabyList(List<Baby> babyList) {
		this.babyList = babyList;
	}
	
}
